package com.jiangsong.batch;

import com.jiangsong.batch.abst.FeeAbstractItemProcessor;
import com.jiangsong.batch.abst.StepSyncManager;
import com.jiangsong.batch.utils.JobParameterUtil;
import com.jiangsong.batch.utils.StepUtil;
import com.jiangsong.entity.Product;
import com.jiangsong.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
public class ProductItemProcessor extends FeeAbstractItemProcessor<Product, Product> {

    @Autowired
    ProductService productService;

    @Override
    public void beforeStep(StepExecution stepExecution) {
        super.beforeStep(stepExecution);
        // 查看jobParamter
        Object myNum = JobParameterUtil.getJobParameter("myNum");
        System.out.println("myNum 是" + myNum);
    }

    @Override
    public Product doBatchService(Product item) {
        String accNo = item.getAccNo();

        log.info("test process begin,输入:{},item:{}", accNo, item);


        List<Product> list1 = productService.queryProductByAcc(accNo);

//
//        boolean dealSuccess = productService.createFeeRateWhenNewSignAcc(accNo);
//        if (!dealSuccess) {
//            log.info("test process end,幂等跳过:{}", accNo);
//            return null;
//        }


        log.info("test process end,处理成功:{}, list1:{}", accNo, list1);

        return item;
    }


    @Override
    public void doBeforeProcess(Product item) {
        super.doBeforeProcess(item);
    }

    @Override
    public void doAfterProcess(Product item) {
        Set<String> feSumSet = new HashSet<>();
        feSumSet.add(item.getAccNo());
        this.addFeeSumNoCache(feSumSet);
    }


    @Override
    public void beforeChunk(ChunkContext chunkContext) {
        log.info("内层chunk 之前,chunk:{}", chunkContext);
        this.removeAllFeeSumNoCache();

    }

    @Override
    public void afterChunk(ChunkContext chunkContext) {
        Set<String> feeSumNoCache = getFeeSumNoCache();
        log.info("内层chunk 之后,feeSumNoCache:{}, chunk:{}", feeSumNoCache, chunkContext);
        this.removeAllFeeSumNoCache();
    }

    @Override
    public void afterChunkError(ChunkContext chunkContext) {

    }

    private void addFeeSumNoCache(Set<String> feSumNoSet) {
        Object feeSumNoCacheValue = StepSyncManager.getContext().getStepExecution().getExecutionContext()
                .get("feeSumNoCacheKey");
        if (null == feeSumNoCacheValue) {
            Set<String> set = new HashSet<>(feSumNoSet);
            StepSyncManager.getContext().getStepExecution().getExecutionContext()
                    .put("feeSumNoCacheKey", set);
            return;
        }
        ((Set<String>) feeSumNoCacheValue).addAll(feSumNoSet);
    }

    private void removeAllFeeSumNoCache() {
        Object feeSumNoCacheValue = StepSyncManager.getContext().getStepExecution().getExecutionContext()
                .get("feeSumNoCacheKey");
        if (null == feeSumNoCacheValue) {
            return;
        }
        StepSyncManager.getContext().getStepExecution().getExecutionContext()
                .remove("feeSumNoCacheKey");
    }

    private Set<String> getFeeSumNoCache() {
        return (Set<String>) StepUtil.getData("feeSumNoCacheKey");
    }
}

